package com.xposed.aliproxy;

import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.xposed.aliproxy.activity.BaseActivity;
import com.xposed.aliproxy.activity.HistoryActivity;
import com.xposed.aliproxy.crash.CrashHandler;
import com.xposed.aliproxy.db.DBUtil;
import com.xposed.aliproxy.http.HttpManager;
import com.xposed.aliproxy.qrcode.GlobalInstance;
import com.xposed.aliproxy.receiver.AliBalanceReceiver;
import com.xposed.aliproxy.receiver.AliLogInOrOutReceiver;
import com.xposed.aliproxy.receiver.AliQRCodeReceiver;
import com.xposed.aliproxy.receiver.AliTradeModelReceiver;
import com.xposed.aliproxy.receiver.AliTradeNoReceiver;
import com.xposed.aliproxy.thread.BatchUploadAliTradeThread;
import com.xposed.aliproxy.thread.CheckNewVersionThread;
import com.xposed.aliproxy.thread.RefreshFloatBallThread;
import com.xposed.aliproxy.utils.ActionUtil;
import com.xposed.aliproxy.utils.ActivityUtil;
import com.xposed.aliproxy.utils.AlertDialogUtil;
import com.xposed.aliproxy.utils.ParamConfig;
import com.xposed.aliproxy.utils.ProgressUtil;
import com.xposed.aliproxy.utils.ToastUtil;
import com.xposed.aliproxy.utils.Utils;
import com.xposed.aliproxy.utils.XposeUtil;

import de.robv.android.xposed.XposedBridge;

public class MainActivity extends BaseActivity {
    private final int f1925r = 1101;
    private final int f1926s = 1102;
    private final int f1927t = 1;
    private int f1928u = 1;
    private boolean f1929v;
    private final String[] f1931x = new String[]{"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE"};


    private MsgReceiver msgReceiver;
    private AliLogInOrOutReceiver aliLogInOrOutReceiver;
    private AliQRCodeReceiver aliQRCodeReceiver;
    private AliTradeNoReceiver aliTradeNoReceiver;
    private AliTradeModelReceiver aliTradeModelReceiver;
    private AliBalanceReceiver aliBalanceReceiver;
    private BatchUploadAliTradeThread batchUploadAliTradeThread;

    private TextView tv_aliAccount;
    private ParamConfig paramConfig;
    private GlobalInstance globalInstance;

    @Override
    public void loadLayout() {
        setContentView(R.layout.activity_main);
    }

    @TargetApi(26)
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setTitle(R.string.app_name);
        this.paramConfig = ParamConfig.getInstance(this.context);
        TextView tv_agentId = (TextView) findViewById(R.id.main_textview_agentId);
        TextView tv_loginAccount = (TextView) findViewById(R.id.main_textview_aliaccount);
        tv_aliAccount = (TextView) findViewById(R.id.main_textview_realaliaccount);
        String agentId = this.paramConfig.getString("agentId", "");
        String agentKey = this.paramConfig.getString("agentKey", "");
        String aliAccount = this.paramConfig.getString("aliAccount", "");
        if (!Utils.isNull(agentId)) {
            tv_agentId.setText(agentId);
        }
        if (!Utils.isNull(aliAccount)) {
            tv_loginAccount.setText(aliAccount);
        }
        findViewById(R.id.main_button_bindalipay).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (bindAli()) {
                        if (HttpManager.getVersionCode(MainActivity.this.context) < Integer.parseInt(MainActivity.this.paramConfig.getString("serverl_version", "0"))) {
                            AlertDialogUtil.showAlertDialog(MainActivity.this.context, "提示", "\n非最新版本，请先升级！\n", "确定", null, null, null);
                            return;
                        }
                        ProgressUtil.showProgressDialog(MainActivity.this.context, false, "绑定支付宝...");
                        Intent launchIntentForPackage = MainActivity.this.getPackageManager().getLaunchIntentForPackage("com.eg.android.AlipayGphone");
                        launchIntentForPackage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        MainActivity.this.startActivity(launchIntentForPackage);
                        MainActivity.this.globalInstance.f1764c = false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.main_button_unbindalipay).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialogUtil.showAlertDialog(MainActivity.this.context, "提示", "\n确定要解绑支付宝吗？\n", "确定", "取消", new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("com.abcd.xpouseali.action.alilogout");
                        intent.putExtra("finish", true);
                        MainActivity.this.sendBroadcast(intent);
                    }
                }, null);
            }
        });
        findViewById(R.id.main_button_list).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.this.startActivity(new Intent(MainActivity.this.context, HistoryActivity.class));
            }
        });
        this.globalInstance = GlobalInstance.getInstance();
        DBUtil dbUtil = DBUtil.getInstance(this.context);
        this.globalInstance.paramConfig = this.paramConfig;
        this.globalInstance.dbUtil = dbUtil;
        register(dbUtil);
        this.paramConfig.remove("local_realaliid");
        CrashHandler.getInstance().init(this);
        this.batchUploadAliTradeThread = new BatchUploadAliTradeThread(this.context, dbUtil);
        this.batchUploadAliTradeThread.start();
        new CheckNewVersionThread(this).start();
        new RefreshFloatBallThread(this).start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        String local_realaliid = this.paramConfig.getString("local_realaliid", "");
        tv_aliAccount.setText(local_realaliid);
        ProgressUtil.closeProgressDialog();
    }

    @Override
    protected void onStop() {
        super.onStop();
        ProgressUtil.closeProgressDialog();
    }

    private void register(DBUtil dBUtil) {
        this.msgReceiver = new MsgReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("kaifu.com.keyinputtool.action.qrdone");
        intentFilter.addAction("kaifu.com.keyinputtool.action.toast");
        intentFilter.addAction("android.intent.action.SCREEN_OFF");
        registerReceiver(this.msgReceiver, intentFilter);
        this.aliLogInOrOutReceiver = new AliLogInOrOutReceiver(this.context);
        intentFilter = new IntentFilter();
        intentFilter.addAction("com.abcd.xpouseali.action.aliloginid");
        intentFilter.addAction("com.abcd.xpouseali.action.aliloginsuccess");
        intentFilter.addAction("com.abcd.xpouseali.action.aliloginfailed");
        intentFilter.addAction("com.abcd.xpouseali.action.alilogout");
        registerReceiver(this.aliLogInOrOutReceiver, intentFilter);
        this.aliQRCodeReceiver = new AliQRCodeReceiver(this.context, dBUtil, this.paramConfig);
        registerReceiver(this.aliQRCodeReceiver, new IntentFilter("com.abcd.xpouseali.action.aliqrcode.received"));
        this.aliTradeNoReceiver = new AliTradeNoReceiver(this.context, dBUtil);
        registerReceiver(this.aliTradeNoReceiver, new IntentFilter("com.abcd.xpouseali.action.alitradeno.received"));
        this.aliTradeModelReceiver = new AliTradeModelReceiver(this.context, dBUtil);
        registerReceiver(this.aliTradeModelReceiver, new IntentFilter(ActionUtil.RECEIVER_TRADE_MODEL));
        this.aliBalanceReceiver = new AliBalanceReceiver(this.context, this.paramConfig);
        registerReceiver(this.aliBalanceReceiver, new IntentFilter(ActionUtil.RECEIVER_ALI_BALANCE));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (this.batchUploadAliTradeThread != null) {
            this.batchUploadAliTradeThread.isAppDestroy = true;
        }
        if (this.msgReceiver != null) {
            unregisterReceiver(this.msgReceiver);
        }
        if (this.aliQRCodeReceiver != null) {
            unregisterReceiver(this.aliQRCodeReceiver);
        }
        if (this.aliLogInOrOutReceiver != null) {
            unregisterReceiver(this.aliLogInOrOutReceiver);
        }
        if (this.aliTradeNoReceiver != null) {
            unregisterReceiver(this.aliTradeNoReceiver);
        }
        if (this.aliTradeModelReceiver != null) {
            unregisterReceiver(this.aliTradeModelReceiver);
        }
        if (this.aliBalanceReceiver != null) {
            unregisterReceiver(this.aliBalanceReceiver);
        }
    }

    private boolean bindAli() {
        try {
            String str = this.context.getPackageManager().getPackageInfo("com.eg.android.AlipayGphone", 0).versionName;
            XposeUtil.xposedLog("aliVerName->", str);
            ToastUtil.showToast(this.context, str);
            return true;
        } catch (Throwable th) {
            XposedBridge.log(th);
            return false;
        }
    }

    class MsgReceiver extends BroadcastReceiver {
        MsgReceiver() {
        }

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("kaifu.com.keyinputtool.action.qrdone")) {
                if (!intent.getBooleanExtra("manualstop", false)) {
                    MainActivity.this.f1929v = true;
                    MainActivity.this.onResume();
                }
            } else if (action.equals("kaifu.com.keyinputtool.action.toast")) {
                Toast.makeText(context, intent.getStringExtra("message"), Toast.LENGTH_SHORT).show();
            } else if (action.equals("android.intent.action.SCREEN_OFF")) {
                MainActivity.this.sendBroadcast(new Intent("com.abcd.xpouseali.action.alilogout"));
            }
        }
    }

    @Override
    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        if (i == 4) {
            AlertDialogUtil.showAlertDialog(this.context, "退出", "\n确定要退出吗？\n", "确定", "取消", new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent("com.abcd.xpouseali.action.alilogout");
                    intent.putExtra("finish", true);
                    MainActivity.this.sendBroadcast(intent);
                    ActivityUtil.closeAll();
                    System.exit(0);
                }
            }, null);
            return true;
        } else if (i == 24) {
            return true;
        } else {
            return super.onKeyDown(i, keyEvent);
        }
    }
}
